package com.peter.baselibrary.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.peter.baselibrary.view.MultipleType;
import com.peter.baselibrary.interfaces.OnItemClickListener;
import com.peter.electronicmall.interfaces.OnItemLongClickListener;

import java.util.ArrayList;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseHolder> {

    protected LayoutInflater mInflater = null;

    private MultipleType<T> mTypeSupport;

    private OnItemClickListener mItemClickListener;

    private OnItemLongClickListener mItemLongClickLintener;

    public int mLayoutId;

    public ArrayList<T> mData;

    /**
     * 单布局
     *
     * @param context   上下文
     * @param mData     数据
     * @param mLayoutId 布局id
     */
    public BaseAdapter(Context context, ArrayList<T> mData, int mLayoutId) {
        mInflater = LayoutInflater.from(context);
        this.mLayoutId = mLayoutId;
        this.mData = mData;
    }

    /**
     * 多布局
     *
     * @param context      上下文
     * @param mData        数据
     * @param mTypeSupport 布局类型
     */
    public BaseAdapter(Context context, ArrayList<T> mData, MultipleType<T> mTypeSupport) {
        this(context, mData, -1);
        this.mTypeSupport = mTypeSupport;

    }

    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mTypeSupport != null) {
            mLayoutId = viewType;
        }
        View view = mInflater.inflate(mLayoutId, parent, false);
        return new BaseHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, final int position) {
        bindData(holder, mData.get(position), position);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(mData.get(position), position);
                }
            });
        }
        if (mItemLongClickLintener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mItemLongClickLintener.onItemLongClick(mData.get(position), position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mTypeSupport != null) {
            return mTypeSupport.getLayoutId(mData.get(position), position);
        } else {
            return super.getItemViewType(position);
        }
    }

    public abstract void bindData(BaseHolder holder, T data, int position);

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public void setOnLongItemClickListener(OnItemLongClickListener mItemLongClickLintener) {
        this.mItemLongClickLintener = mItemLongClickLintener;
    }
}
